<template>
  <a_page>
    <a页头固定栏>
      <a工具栏 @keyup.enter="选型">
        <a-q-input outlined dense v-model.number="req单项.流量" label="流量" placeholder="流量……" :wd="150" />
        <a-q-input outlined dense v-model.number="req单项.扬程" label="扬程" placeholder="扬程……" :wd="150" />
        <q-btn dense text-color="primary" label="选型" icon="search" @click="选型" />
      </a工具栏>
    </a页头固定栏>

    <div class="row q-gutter-sm q-pa-sm flex-center">
      <q-card class="选型 q-pa-md" v-if="!Object.keys(单项集).length">
        没有数据……
      </q-card>
      <q-card class="q-pa-md 选型" v-for="(单项组, key) in 单项集" :key="key">
        <chart :单项组="单项组" :名称="key" />
      </q-card>
    </div>
  </a_page>
</template>

<script setup lang="ts">
import chart from "./chart.vue"
import { ref, reactive, computed, watch } from 'vue'
import *  as util from '@/utils'
import a列表字段 from '@/components/a列表字段.vue';
import AQInput from '@/amtf/components/a-q-input.vue';
const req单项 = reactive({
  流量: 168,
  扬程: 88
})
import useApi from "@/api/增查api"
const api = useApi()

// const 单项组 = reactive<单项组>([])
const 单项集 = reactive<单项>({})
const 插值单项集 = reactive<单项>({})

async function 选型() {
  const res = await api.查(req单项)
  console.log(`查多个 res 👉`, res)
  if (res.code === 0) {
    // util.数组重予(单项组, res.data.list)
    util.对象重予(单项集, res.data)
    // 插值计算()
  }
}

function 插值计算() {
  const 流量 = 300;
  const 扬程 = 90;
  util.对象重予(插值单项集, {})
  for (const 泵型号 in 单项集) {
    const pumpData = 单项集[泵型号];
    let closestData1 = null;
    let closestData2 = null;
    let minDiff1 = Infinity;
    let minDiff2 = Infinity;

    // 找到基于流量最接近的两个数据点
    for (const item of pumpData) {
      const diff = Math.abs(item.流量 - 流量);
      if (diff < minDiff1) {
        closestData2 = closestData1;
        minDiff2 = minDiff1;
        closestData1 = item;
        minDiff1 = diff;
      } else if (diff < minDiff2) {
        closestData2 = item;
        minDiff2 = diff;
      }
    }

    if (closestData1 && closestData2) {
      const allHeads = pumpData.map(item => item.扬程);
      const minHead = Math.min(...allHeads);
      const maxHead = Math.max(...allHeads);

      // 检查目标扬程是否在数据范围内
      if (扬程 < minHead || 扬程 > maxHead) {
        console.log(`泵型号 ${泵型号} 中，目标扬程 ${扬程} 不在数据范围内，无法进行插值计算。`);
        continue;
      }

      const flowDiff = closestData2.流量 - closestData1.流量;
      const flowWeight = (流量 - closestData1.流量) / flowDiff;
      const interpolatedEfficiency = closestData1.效率 + flowWeight * (closestData2.效率 - closestData1.效率);
      const interpolatedNPSHr = closestData1.NPSHr + flowWeight * (closestData2.NPSHr - closestData1.NPSHr);

      console.log(`泵型号 ${泵型号} 中，对于流量 ${流量} 和扬程 ${扬程} 的插值计算结果：`);
      console.log(`效率：${interpolatedEfficiency}`);
      console.log(`NPSHr：${interpolatedNPSHr}`);
      插值单项集[泵型号] = { 效率: interpolatedEfficiency }
    } else {
      console.log(`泵型号 ${泵型号} 中没有足够的数据点进行插值计算`);
    }
    console.log(`插值单项集 👉`, 插值单项集)
  }
}


</script>
